package com.yyge.web.servlet;
import com.yyge.entity.BaseEntity;
import com.yyge.entity.Employee;
import com.yyge.service.EmployeeService;
import com.yyge.service.impl.EmployeeServiceImpl;
import com.yyge.util.ServletUtil;
import com.yyge.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/select/")
public class EmployeeServlet extends HttpServlet {
    private EmployeeService service=new EmployeeServiceImpl();
    private static Logger logger= LoggerFactory.getLogger(EmployeeServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将request请求中获取url参数，并构建为GoodsInfoDto的对象
        // GoodsInfoDto对象中包含了分页查询的条件和页码等参数
        Employee base= ServletUtil.parameter2dto(req,Employee.class);
        R r=service.selectAll(base);
        ServletUtil.writeResult2Client(resp,r);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 从请求中提取前端提交的数据，并将数据转换为Employee的对象
        // 注意： 前端提交的属性必须和Employee中的属性名完全一致，不然不能够将前端提交的参数转换为目标对象
        BaseEntity baseEntity = ServletUtil.fetchDataFromRequest(req,Employee.class);

        Map<String, Object> params = baseEntity.getParams();
        Employee targrt = (Employee) baseEntity;
        // 从params中获取部门id
        // if (params.containsKey("dept_id")) {
        //     Integer dept = (Integer) params.get("dept_id");
        //     logger.debug(dept+"-----------------");
        //     // 将部门编号转换为一个SysDept的对象
        //     sys_dept sysDept = new sys_dept();
        //     sysDept.setDeptId(dept.longValue());
        //     targrt.setDept(sysDept);
        // }

        R r = null;
        Integer postType = (Integer)params.get("type");
        if(postType==1/*新增&&修改*/){
            if (targrt.get_____() == null) {
                // targrt.setCreateTime(new Date());// 设置新增用户的时间
                 r = service.insert(targrt);
               }else {
                // targrt.setUpdateTime(new Date());
                r = service.updateByPrimaryKeySelective(targrt);
            }
        }
        if(postType==2/*删除*/){
          Integer deleteKey=(Integer)params.get("key");
          r = service.deleteByPrimaryKey(deleteKey);
        }
        if(postType==3/*批量删除*/){
            R l=new R();
            List<Integer> list=(List<Integer>) params.get("list");
            List<Integer> code=new ArrayList<>();
            Integer i = 1;
            String t="";
            for (int j = 0; j < list.size(); j++) {
              Integer number = service.deleteByPrimaryKey(list.get(j)).getCode();
              if(number!=20000){
                  i=-1;
                  t=list.get(j)+"";
              }
            }

            if(i==-1){
                l.setCode(50000);
                l.setMessage("操作失败 "+t);
            }else{
                l.setCode(20000);
                l.setMessage("操作成功");
            }
            r=l;
        }

        ServletUtil.writeResult2Client(resp,r);
    }
}
